{extend name="base"/}
{block name="resources"}
<style>
	.ns-form {
		margin-top: 0;
	}
</style>
{/block}
{block name="main"}
<form class="layui-form ns-form">
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>品牌名称：</label>
		<div class="layui-input-block">
			<input name="brand_name" type="text" value="{$brand_info['brand_name']}" lay-verify="required" class="layui-input ns-len-long" autocomplete="off">
		</div>
	</div>
	
	<div class="layui-form-item">
		<label class="layui-form-label">品牌首字母：</label>
		<div class="layui-input-block">
			<input name="brand_initial" type="text" value="{$brand_info['brand_initial']}" maxlength="1" lay-verify="brand_initial" class="layui-input ns-len-short" autocomplete="off">
		</div>
		<div class="ns-word-aux">
			<p>用于解决某些生僻字无法正确生成品牌首字母的情况</p>
		</div>
	</div>
	
	<div class="layui-form-item">
		<label class="layui-form-label">图片：</label>
		<div class="layui-input-block">
			<div class="upload-img-block">
				<div class="upload-img-box" id="imgUpload">
					{notempty name="$brand_info['image_url']"}
					<img src="{:img($brand_info['image_url'])}"/>
					{else/}
					<div class="ns-upload-default">
						<img src="__STATIC__/img/upload_img.png" />
						<p>点击上传</p>
					</div>
					{/notempty}
				</div>
			</div>
		</div>
		<div class="ns-word-aux">
			<p>建议图片尺寸：200px * 100px。</p>
			<p>图片格式：jpg、png、jpeg。</p>
		</div>
	</div>
	
	<div class="layui-form-item">
		<label class="layui-form-label">排序：</label>
		<div class="layui-input-block">
			<input name="sort" type="number" value="{$brand_info['sort']}" placeholder="请输入排序" lay-verify="num" class="layui-input ns-len-short" autocomplete="off">
		</div>
		<div class="ns-word-aux">
			<p>排序值只能为整数</p>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">是否推荐：</label>
		<div class="layui-input-inline">
			<input type="checkbox" name="is_recommend" lay-skin="switch" value="1" {if $brand_info.is_recommend ==1 }checked{/if}>
		</div>
	</div>

	<input type="hidden" name="brand_id" value="{$brand_info['brand_id']}">
	
	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
		<button type="reset" class="layui-btn layui-btn-primary" onclick="back()">返回</button>
	</div>
</form>
{/block}
{block name="script"}
<script>
	layui.use(['form','upload'], function() {
		var form = layui.form,
			upload = layui.upload,
			file = "{$brand_info['image_url']}",
			repeat_flag = false;//防重复标识
		form.render();

		/**
		 * 表单验证
		 */
		form.verify({
			brand_initial: function (value) {
				if (value.length > 0 && value.length != 1) {
					return '品牌首字母必须是单个字母';
				}
			},
			num: function (value) {
				if (value == '') {
					return;
				}
				if (value%1 != 0) {
					return '排序数值必须为整数';
				}
				if (value < 0) {
					return '排序数值必须为大于0';
				}
			}
		});
		
		//普通图片上传
		var uploadInst = upload.render({
			elem: '#imgUpload', 
			url: ns.url("admin/upload/upload"), 
			done: function (res) {
				if (res.code >= 0) {
					file = res.data.pic_path;
					$("#imgUpload").html("<img src=" + ns.img(res.data.pic_path) + " >");
				}
				return layer.msg(res.message);
			}
		});
		
		form.on('submit(save)', function (data) {
			
			data.field.image_url = file;
			if (repeat_flag) return false;
			repeat_flag = true;
			
			$.ajax({
				url: '{:addon_url("admin/goodsbrand/editBrand")}',
				data: data.field,
				dataType: 'json',
				type: 'post',
				success: function (res) {
					repeat_flag = false;
					
					if (res.code == 0) {
						layer.confirm('编辑成功', {
							title:'操作提示',
							btn: ['返回列表', '继续操作'],
							yes: function(){
								location.href = ns.url("admin/goodsbrand/lists")
							},
							btn2: function() {
								location.reload();
							}
						})
					}else{
						layer.msg(res.message);
					}
				}
			});
			return false;
		});
	});
	
	function back(){
		location.href = ns.url("admin/goodsbrand/lists")
	}
</script>
{/block}